﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLMS.DAL;
using OLMS.BLL;

namespace OLMS
{
    public partial class OrderItem : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OrderDetailBinder();
            }
        }
     
        private void OrderDetailBinder()
        {
            int orderId = 0;
            int userId = 0;
            if (Request["oid"] != null)
            {
              
                orderId = Convert.ToInt32(Request["oid"]);
                btnOrder.Visible = false;
                txtUserName.Enabled = false;
                txtAddress.Enabled = false;
                txtEmail.Enabled = false;
                txtPhone.Enabled = false;
                txtPostalCode.Enabled = false;
            }

            Orders order = OrderBLL.GetOrdersById(orderId);
            if (orderId == 0)
            {
                Users user = Session["user"] as Users;
                if (user == null)
                {
                    Response.Redirect("Login.aspx");
                }
                else
                {
                    userId = user.UserId;
                    order.Username = user.UserName;
                    order.Phone = user.Phone;
                    order.Total = CartBLL.GetTotal(userId);
                }
            }

            lblOrderDate.Text = string.Format("下单日期：{0:D}", order.OrderDate);
            txtUserName.Text = order.Username;
            txtAddress.Text = order.Address;
            txtEmail.Text = order.Email;
            txtPhone.Text = order.Phone;
            txtPostalCode.Text = order.PostalCode;

            List<OrderDetails> orderDetailsList = OrderBLL.GetOrderDetailsByOrderId(orderId, userId);
            var odList = orderDetailsList.Select(od => new
            {
                OrderDetailId = od.OrderDetailId,
                OrderId = od.OrderId,
                AlbumId = od.AlbumId,
                Quantity = od.Quantity,
                UnitPrice = od.UnitPrice,
                AlbumName = AlbumBLL.GetAlbumsById(od.AlbumId).Title,
                ArtistName = AlbumBLL.GetAlbumsById(od.AlbumId).Artists.Name,
                TotalPrice = string.Format("{0:C}元", od.Quantity * od.UnitPrice)
            }).ToList();
            repOrderDetailList.DataSource = odList;
            repOrderDetailList.DataBind();
            lblTotal.Text = string.Format("总计：{0:C}元", order.Total);
        }


        protected void btnOrder_Click(object sender, EventArgs e)
        {
            Users user = Session["user"] as Users;
            Orders orders = new Orders();
            orders.Username = txtUserName.Text;
            orders.OrderDate = DateTime.Now;
            orders.Phone = txtPhone.Text;
            orders.Email = txtEmail.Text;
            orders.PostalCode = txtPostalCode.Text;
            orders.Address = txtAddress.Text;
            orders.OrderStatus = 0;
            orders.Total = CartBLL.GetTotal(user.UserId);
            if (OrderBLL.AddOrder(user.UserId, orders))
            {
                Response.Redirect("Index.aspx");
            }
            else
            {
                Response.Write("<script>alert('下单错误！');</script>");
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (Request["oid"]==null)
            {
                Response.Redirect("Cart.aspx");
            }
            else
            {
                Response.Redirect("Order.aspx");
            }

        }
    }
}